package com.yxc.studiotest2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.yxc.eslibrary.http.*;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.ParseException;
import org.apache.http.message.BasicHeader;

import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    static class Bean{
        long time;
        String status;
    }

    private static final String TAG = "MainActivity";
    ArrayList<Bean> times = new ArrayList<Bean>();

    Date date;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        doFinalRequest();
//        doXUtilsRequest();
        doESRequest();
    }

    private void doESRequest(){
        HttpRequest request = new HttpRequest();
        IHttpParams params = new HttpParams();
        params.addParams("num", "10");
        params.addParams("page", "1");
        HttpHeader header = new HttpHeader("apikey", "ac9c1c27e1997fc2e8e4e6a27a70aae8");
        request.get("http://apis.baidu.com/txapi/tiyu/tiyu", new HttpHeader[]{header}, params, new HttpCallBack() {
            @Override
            public void requestStarted() {

                date = new Date();
            }

            @Override
            public void requestSucceed(String result) {

                Log.d(TAG, result.substring(0, 40));
                continueRequest(true);
            }

            @Override
            public void requestFailed(HttpResponse response) {
                Log.d(TAG, response.getMessage());
            }
        });
    }


    private void doFinalRequest(){
        FinalHttp http = new FinalHttp();
        Header header = new BasicHeader("apikey", "ac9c1c27e1997fc2e8e4e6a27a70aae8");
        Header[] headers = new Header[]{header};
        http.get("http://apis.baidu.com/txapi/tiyu/tiyu?num=10&page=1", headers, null, new AjaxCallBack<String>() {

            @Override
            public void onStart() {
                super.onStart();
                date = new Date();
            }

            @Override
            public void onSuccess(String s) {
                super.onSuccess(s);
                Log.d(TAG, s.substring(0, 40));
                continueRequest(true);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Log.d(TAG, strMsg);
                continueRequest(false);
            }
        });
    }

    private void continueRequest(boolean success){
        Date end = new Date();
        long time = end.getTime() - date.getTime();
        Bean bean = new Bean();
        bean.status = success?"成功":"失败";
        bean.time = time;
        times.add(bean);
        if (times.size()==20){
            printLog();
            return;
        }
        date = null;
//        doFinalRequest();
//        doXUtilsRequest();
        doESRequest();
    }

    private void printLog(){
        long total = 0;
        StringBuffer sb = new StringBuffer();
        for (Bean bean: times){
            total+=bean.time;
            sb.append(bean.status).append("：").append(bean.time).append("\t");
        }
        double pj = total*1.0/times.size();
        Log.d(TAG, sb.toString());
        Log.d(TAG, "平均值："+pj);
    }

    private void doXUtilsRequest(){
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addHeader("apikey", "ac9c1c27e1997fc2e8e4e6a27a70aae8");
        httpUtils.send(com.lidroid.xutils.http.client.HttpRequest.HttpMethod.GET, "http://apis.baidu.com/txapi/tiyu/tiyu?num=10&page=1",
                params, new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        date = new Date();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Log.d(TAG, responseInfo.result.substring(0, 40));

                        continueRequest(true);
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                        Log.d(TAG, s);
                        continueRequest(false);
                    }
                });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
